package im.elephas.androfan.activity;

import im.elephas.androfan.R;
import im.elephas.androfan.common.widget.SimpleStatusWidget;
import im.elephas.androfan.fanfouapi.FanfouApi;
import im.elephas.androfan.fanfouapi.events.IChangeEvent;
import im.elephas.androfan.fanfouapi.events.IErrorEvent;
import im.elephas.androfan.fanfouapi.events.IStatusesEvent;
import im.elephas.androfan.fanfouapi.items.StatusItem;
import im.elephas.androfan.fanfouapi.items.UserItem;
import im.elephas.androfan.util.DateFormater;
import im.elephas.androfan.util.ImageGetter;
import im.elephas.androfan.util.TagFilter;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class PersonalInfoActivity extends AndroFanActivity implements
		IStatusesEvent, OnClickListener {

	private Button button;
	private TextView countTextView;
	private TextView followersTextView;
	private TextView friendsTextView;
	private ImageView imageView;
	private TextView nameTextView;

	private ProgressBar progressBar;

	private LinearLayout statusesLinearLayout;
	private UserItem userItem;
	private boolean isFriend;
	private List<StatusItem> listStatusesItems;

	@Override
	public void onClick(View v) {
		button.setEnabled(false);
		if (isFriend) {
			getFanfouApi().destroyFriendship(userItem.getId(),
					new IChangeEvent() {

						@Override
						public void changeHandler(boolean success) {
							getActivity().runOnUiThread(new Runnable() {

								@Override
								public void run() {
									Toast.makeText(getActivity(), "撤销成功",
											Toast.LENGTH_SHORT).show();
									isFriend = false;
									button.setText("加为好友");
									button.setEnabled(true);
								}
							});
						}
					}, getApiCaller().mIErrorEvent);
		} else {
			getFanfouApi().createFriendship(userItem.getId(),
					new IChangeEvent() {

						@Override
						public void changeHandler(boolean success) {
							getActivity().runOnUiThread(new Runnable() {

								@Override
								public void run() {
									Toast.makeText(getActivity(), "加入成功",
											Toast.LENGTH_SHORT).show();
									isFriend = false;
									button.setText("撤销好友");
									button.setEnabled(true);
								}
							});
						}
					}, getApiCaller().mIErrorEvent);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		refreshStatuses();
	}

	@Override
	public void statusesHandler(final List<StatusItem> lsi) {
		listStatusesItems = lsi;
		getActivity().runOnUiThread(new Runnable() {

			@Override
			public void run() {
				refreshStatuses();
			}
		});
	}

	private void refreshStatuses() {
		progressBar.setVisibility(View.GONE);

		boolean colorDark = false;
		for (final StatusItem item : listStatusesItems) {
			SimpleStatusWidget ssw = new SimpleStatusWidget(
					statusesLinearLayout, getActivity(), item);

			if (colorDark)
				ssw.getWidgetView().setBackgroundColor(
						Color.argb(255, 202, 222, 235));
			else
				ssw.getWidgetView().setBackgroundColor(
						Color.argb(255, 237, 237, 237));
			colorDark = !colorDark;

			ssw.addToParentView();
		}
	}

	private void refreshUi() {
		setContentView(R.layout.personalinfo);
		button = (Button) findViewById(R.id.personalButton);
		countTextView = (TextView) findViewById(R.id.personalCountTextView);
		followersTextView = (TextView) findViewById(R.id.personalFollowersTextView);
		friendsTextView = (TextView) findViewById(R.id.personalFriendsTextView);
		imageView = (ImageView) findViewById(R.id.personalImageView);
		nameTextView = (TextView) findViewById(R.id.personalNameTextView);
		statusesLinearLayout = (LinearLayout) findViewById(R.id.personalStatusesLinearLayout);
		progressBar = (ProgressBar) findViewById(R.id.personalProgressBar);

		countTextView.setText(userItem.getStatusesCount() + "\n消息");
		followersTextView.setText(userItem.getFollowersCount() + "\n被关注");
		friendsTextView.setText(userItem.getFriendsCount() + "\n关注的");
		nameTextView.setText(userItem.getScreenName());
		imageView.setImageBitmap(ImageGetter.getBitMap(userItem
				.getProfileImageUrl()));

		List<String> lu = getFanfouApi().getFirendsListIds();
		if (lu.contains(userItem.getId())) {
			isFriend = true;
			button.setText("撤销好友");
		} else {
			isFriend = false;
			button.setText("加为好友");
		}

		button.setOnClickListener(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		userItem = (UserItem) getIntent().getSerializableExtra("item");

		setTitle("@" + userItem.getScreenName() + "的详细资料");

		refreshUi();
		getFanfouApi().getUserTimeline(10, userItem.getId(), this,
				getApiCaller().mIErrorEvent);
	}

}